const app=getApp()
const userDB=wx.cloud.database().collection("user")
const applyDB=wx.cloud.database().collection("apply")
const _ = wx.cloud.database().command
var nickName
var avatarUrl
var dakaDay

Page({
  data: {
    datalist:[],
    timestamp:'',
    name:[], //用户昵称
  },

  getName:function(e)
  {
    this.setData({
      name:e.detail.value
    });
  },

  onLoad: function () {
    let that=this
    userDB.where({
      friend:"暂无",
      _openid:_.neq(app.globalData.openid)
    })
    .get({
      success:res=>{
        console.log(res.data)
        that.setData({
          datalist:res.data,
        })
      }
    })
  },

  //申请病友按钮
  shenqing(event){
    let that=this
    that.getTime()
    //console.log("点击申请按钮会发生什么事情",event.currentTarget.dataset)
    var friendOpenid=event.currentTarget.dataset.openid

    userDB.where({
      _openid:app.globalData.openid,
    }).get({
      success:res=>{
        console.log(res.data[0].nickName)
        nickName=res.data[0].nickName
        avatarUrl=res.data[0].avatarUrl
        dakaDay=res.data[0].dakaDays

        applyDB.add({
          data:{
            sendOpenid:app.globalData.openid,
            receiveOpenid:friendOpenid,
            sendName:nickName,
            sendImg:avatarUrl,
            sendDay:dakaDay,
            timestamp:that.data.timestamp,
          }
        })
      }
    })

    wx.showToast({
      title: '发送申请成功',
      icon: 'succes',
      duration: 1000,
      success:function(){
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          });
        },2000);
      }
  })
  },

  search(){
    let that=this
    wx.navigateTo({
      url: '/pages/applyfriend/search?name='+that.data.name,
    })
  },

  getTime:function(){
    var curtime = Date.parse(new Date());
    curtime = curtime / 1000;
    //获取当前时间  
    var n = curtime * 1000;
    var date = new Date(n);
    //年，月，日，时，分，秒
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    this.data.timestamp=Y +'/'+ M+'/' + D +' '+ h + ":" + m + ":" + s
  },

})